package com.yifeng.repo.base.utils.action.impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yifeng.repo.base.utils.action.BizCacheAction;

import java.util.concurrent.TimeUnit;

/**
 * Created by daibing on 2024/2/26.
 */
public class BizGuavaCacheAction implements BizCacheAction {
    private final Cache<String, String> localCache;

    public BizGuavaCacheAction(int initialCapacity, long localMaximumSize, long localDurationSeconds) {
        this.localCache = CacheBuilder.newBuilder()
                .initialCapacity(initialCapacity)
                .maximumSize(localMaximumSize)
                .concurrencyLevel(16)
                .expireAfterWrite(localDurationSeconds, TimeUnit.SECONDS)
                .build();
    }

    @Override
    public String get(String key) {
        return localCache.getIfPresent(key);
    }

    @Override
    public void put(String key, long seconds, String value) {
        localCache.put(key, value);
    }
}
